﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.ComponentModel.DataAnnotations;
using WpfApplication2.Helpers;

namespace WpfApplication2.ViewModels
{

    public class ViewModel : INotifyPropertyChanged
    {
        public void UpdateValidationList(PropertyElement pe)
        {
            bool flag = false;
            foreach (PropertyElement item in ValidationList)
            {
                if (item.Name == pe.Name)
                {
                    flag = true;
                    item.Name = pe.Name;
                }
            }
            if (flag == false)
                ValidationList.Add(pe);
        }

        public bool DataIsValid
        {
            get
            {
                bool isValid = true;
                RaisePropertyChanged("ValidationList");
                foreach (PropertyElement valp in ValidationList)
                {   
                    ValidacionParticular(valp);
                    isValid = isValid && valp.IsValid;
                }
                return isValid;
            }
        }

        public bool ValidacionParticular(PropertyElement valp)
        {
            bool flag = true;
            foreach (ValidationType valt in valp.Validations)
            {
                switch (valt)
                {
                    case ValidationType.Requerido:
                        flag = flag && PropertyValidator.ValidarRequired(valp);
                        break;
                    case ValidationType.SoloNumeros:
                        flag = flag && PropertyValidator.ValidarSoloNumeros(valp);
                        break;
                    case ValidationType.Telefono:
                        break;
                    default:
                        break;
                }

            }
            if (!flag)
            {
                //ConsoleColor.
                valp.Foreground = "#bb0000";
                RaisePropertyChanged(valp.Name);
                valp.IsValid = false;
                return false;
            }
            else
            {
                valp.Foreground = "White";
                RaisePropertyChanged(valp.Name);
                valp.IsValid = true;
                return true;
            }
        }

        private List<PropertyElement> validationList;
        public List<PropertyElement> ValidationList
        { 
            get 
            {
                if (validationList == null)
                    validationList = new List<PropertyElement>();
                return validationList;
            }
            set 
            {
                validationList = value;
            }
        }

        private Dictionary<string, bool> validations = new Dictionary<string, bool>();
        public bool CanValidate { get; set; }

        
        public bool IsDesignTime
        {
            get
            {
                return (Application.Current == null) || (Application.Current.GetType() == typeof(Application));
            }
        }
        protected bool IsValid { get; set; }

        public event PropertyChangedEventHandler PropertyChanged = delegate
        {
        };
        public event Action<Object, ViewModel> MustShowChildWindow;
        public void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void ShowChildWindow(ViewModel data)
        {
            MustShowChildWindow(this, data);
        }

        public virtual void ChildWindowClosed(ViewModel data)
        {
        }

        protected void ClearValidationStatus()
        {
            this.CanValidate = false;
            validations = new Dictionary<string, bool>();
        }

        
        private bool CheckValidations()
        {
            foreach (bool validation in validations.Values)
                if (!validation)
                    return false;

            return true;
        }

    }
}
